<!DOCTYPE html>
<html lang="en">
<head>　
    <meta charset="UTF-8"/>
    <meta http-equiv=X-UA-Compatible content="IE=edge"/>
    <meta name=viewport content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    　　<title>密码修改</title>　
    <link rel="stylesheet" href="css/element/index.css"/>
    <link rel="stylesheet" href="css/main.css"/>
    <link rel="stylesheet" href="css/header.css"/>
    <link rel="stylesheet" href="css/sidebar.css"/>
    <link rel="stylesheet" href="css/right.css"/>
    <link rel="stylesheet" href="css/password.css"/>
</head>
<body>
<div id="app" class="password">
    <div class="ms-title">密码修改</div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="70px" class="demo-ruleForm">
        <el-form-item label="旧密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPass">
            <el-input type="password" v-model="ruleForm.newPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item class="password-btn">
            <el-button type="primary" @click="submitForm('ruleForm')">确认修改</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <span style="color: red">登录密码修改规则说明：<br/><br/>

1.密码长度为6~16位，至少包含数字、字母区分大小写<br/><br/>

2.密码不可与账号相同<br/><br/>

3.密码不可以与身份证号后几位相同<br/><br/>
4.旧密码不可以与新密码相同</span>
    </el-form>
</div>
<script src="js/vue.min.js"></script>
<script src="js/element.js"></script>
<script src="js/axios.min.js"></script>
<script src="js/basic.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            let validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入旧密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            let validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            let validateNewPass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入新密码'));
                } else {
                    if (this.ruleForm.newPass.length < 6) {
                        callback(new Error('密码必须大于5位'));
                    } else if (value === this.ruleForm.pass) {
                        callback(new Error('新密码不能与旧密码一样'));
                    }
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    newPass: ''
                },
                rules: {
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                    newPass: [
                        {validator: validateNewPass, trigger: 'blur'}
                    ]

                },
                submitForm(formName) {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            //获取登陆者的身份
                            let radio = localStorage.getItem("radio")
                            //获取登陆者的id
                            let adNo = localStorage.getItem("adNo")
                            //获取登陆者的id
                            let id = localStorage.getItem("id")
                            let formData = new FormData();
                            let Url = 'http://localhost:8080/'
                            if (radio === '学生') {
                                Url = Url + 'login/student/update';
                                formData.append("pass", this.ruleForm.pass);
                                formData.append("id", id);
                                formData.append("newPass", this.ruleForm.newPass);
                            } else if (radio === '教师') {
                                Url = Url + 'login/teacher/update';
                                formData.append("pass", this.ruleForm.pass);
                                formData.append("id", id);
                                formData.append("newPass", this.ruleForm.newPass);
                            } else {
                                Url = Url + 'login/admin/update';
                                formData.append("pass", this.ruleForm.pass);
                                formData.append("adNo", adNo);
                                formData.append("newPass", this.ruleForm.newPass);
                            }
                            axios.post(Url, formData)
                                .then(res => {
                                    let r = res.data;
                                    if (r.code !== 200) {
                                        this.$message.error(r.msg);
                                    } else {
                                        this.$message({
                                            message: r.msg,
                                            type: 'success'
                                        });
                                    }
                                });
                        } else {
                            return false;
                        }
                    });
                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                }
            }
        }
    })
</script>

　
</body>
</html>
